package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.model.domain.UserLocation;
import javafx.scene.effect.Light;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metrics;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

/**
 * @Description: test
 * @Create by: JJ菜菜
 * @Date: 2021/12/7 18:09
 */

@DubboService
public class UserLocationApiImpl implements UserLocationApi{

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 更新地理环境
     * @param userId
     * @param longitude
     * @param latitude
     * @param addrStr
     * @return
     */
    @Override
    public Boolean updateLocation(Long userId, Double longitude, Double latitude, String addrStr) {

        try {
            // 查询用户的地理位置
            Query query = Query.query(Criteria.where("userId").is(userId));
            UserLocation userLocation = mongoTemplate.findOne(query, UserLocation.class);
            if(userLocation == null){
                // 如果不存在就保存
                userLocation = new UserLocation();
                userLocation.setUserId(userId);
                // 构建经纬度
                userLocation.setLocation(new GeoJsonPoint(longitude, latitude));
                userLocation.setAddress(addrStr);
                userLocation.setUpdated(System.currentTimeMillis());
                userLocation.setCreated(System.currentTimeMillis());
                userLocation.setLastUpdated(System.currentTimeMillis());
                mongoTemplate.save(userLocation);
            }else {
                // 如果存在就更新用户的地理位置
                Update update = Update.update("location", new GeoJsonPoint(longitude, latitude))
                        .set("updated", System.currentTimeMillis())
                        .set("lastUpdated", System.currentTimeMillis());
                mongoTemplate.updateFirst(query, update, UserLocation.class);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 查询附近的用户id
     * @param id
     * @param metra
     * @return
     */
    @Override
    public List<Long> searchNearUser(Long id, Double metra) {

        // 1. 根据当前的用户id，查询用户的信息
        Query query = Query.query(Criteria.where("userId").is(id));
        UserLocation userLocation = mongoTemplate.findOne(query, UserLocation.class);
        if(userLocation == null){
            return null;
        }
        // 2. 以当前用户id绘制原点
        GeoJsonPoint point = userLocation.getLocation();
        // 3. 绘制半径
        Distance distance = new Distance(metra, Metrics.KILOMETERS);
        // 4. 绘制圆形
        Circle circle = new Circle(point, distance);
        // 5. 查询
        // location条件为构造成的圆开始搜索
        Query queryLocation = Query.query(Criteria.where("location").withinSphere(circle));
        List<UserLocation> userLocationList = mongoTemplate.find(queryLocation, UserLocation.class);
        List<Long> userids = CollUtil.getFieldValues(userLocationList, "userId", Long.class);
        return userids;
    }
}
